package com.wish233.wiliwilivideo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wish233.wiliwilicommon.common.BaseResponse;
import com.wish233.wiliwilicommon.common.ErrorCode;
import com.wish233.wiliwilicommon.common.ResultUtils;
import com.wish233.wiliwilicommon.exception.BusinessException;
import com.wish233.wiliwilicommon.exception.ThrowUtils;
import com.wish233.wiliwilivideo.domain.dto.video.VideoCreateRequest;
import com.wish233.wiliwilivideo.domain.dto.video.VideoQueryRequest;
import com.wish233.wiliwilivideo.domain.dto.video.VideoUpdateRequest;
import com.wish233.wiliwilivideo.domain.po.Video;
import com.wish233.wiliwiliapi.domain.vo.VideoVO;
import com.wish233.wiliwilivideo.service.VideoService;
import com.wish233.wiliwilivideo.utils.BloomFilterUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author WHH
 */
@RestController
public class VideoController {

    @Resource
    private VideoService videoService;
    @Resource
    private BloomFilterUtils bloomFilterUtils;

    @ApiOperation("创建视频")
    @PostMapping("/create")
    public BaseResponse<Long> createVideo(@RequestBody VideoCreateRequest videoCreateRequest) {
        if (videoCreateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long videoId = videoService.createVideo(videoCreateRequest);
        return ResultUtils.success(videoId);
    }

    @ApiOperation("更新视频信息")
    @PutMapping()
    public BaseResponse<Boolean> updateVideo(@RequestBody VideoUpdateRequest videoUpdateRequest) {
        if (videoUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = videoService.updateVideo(videoUpdateRequest);
        return ResultUtils.success(result);
    }

    @ApiOperation("删除视频")
    @DeleteMapping("/{videoId}")
    //@SaCheckRole("ADMIN")
    public BaseResponse<Boolean> deleteVideo(@PathVariable Long videoId) {
        if (videoId == null || videoId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = videoService.deleteVideo(videoId);
        return ResultUtils.success(result);
    }

    @ApiOperation("分页查询视频")
    @PostMapping("/list")
    public BaseResponse<List<VideoVO>> queryVideoByPage(@RequestBody VideoQueryRequest videoQueryRequest) {
        ThrowUtils.throwIf(videoQueryRequest == null, new BusinessException(ErrorCode.PARAMS_ERROR));
        int current = videoQueryRequest.getCurrent();
        int pageSize = videoQueryRequest.getPageSize();
        Page<Video> page = videoService.page(new Page<>(current, pageSize), videoService.getQueryWrapper(videoQueryRequest));
        return ResultUtils.success(page.getRecords().stream().map((video) -> BeanUtil.copyProperties(video, VideoVO.class)).collect(Collectors.toList()));
    }
    @ApiOperation("根据id查询视频")
    @GetMapping("/{id}")
    public BaseResponse<VideoVO> getVideoById(@PathVariable Long id) {
        ThrowUtils.throwIf(id == null, new BusinessException(ErrorCode.PARAMS_ERROR));
        return ResultUtils.success(videoService.getVideoById(id));
    }

    @ApiOperation("获取所有视频（内部调用）")
    @GetMapping("/all")
    public List<VideoVO> getAllVideo() {
        return videoService.getAllVideo();
    }

    @GetMapping("/test")
    public String kong() {
        return "k";
    }
}
